import { quest } from "../../utils/http";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: [],
    index: 0,
    size: 10,
    data: [],
    advert: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */

  async onLoad(options) {
    let ad = await quest({
      url: "advert/list",
    });
    if (ad.code == 200) {
      this.setData({
        advert: ad.data.items,
      });
    }
    let title = [];
    title.push({ id: 0, deliveryAddr: "全部", select: true });

    // 检查是否登录
    let token = wx.getStorageSync("token");
    if (token == "") {
      wx.showModal({
        title: "提示",
        content: "请先登录",
        success(res) {
          if (res.confirm) {
            wx.switchTab({
              url: "/pages/me/me",
            });
          } else if (res.cancel) {
            console.log("用户点击取消");
          }
        },
      });
      this.setData({
        title,
      });
      return;
    }

    // 设置标题栏
    this.getTitleData();
    this.setData({
      index: 0,
    });
    await this.getDataItems(this.data.size);
  },

  async activeTitle(e) {
    if (this.data.title.length == 1) {
      return;
    }
    let { index } = e.target.dataset;
    console.log(index);

    //切换点击标题效果
    let title = this.data.title;
    title.forEach((item) => {
      item.select = false;
      if (item.id == index) {
        item.select = true;
      }
    });
    // 判断是否为全部订单
    if (index == 0) {
      await this.getDataItems(10);
      this.setData({
        title,
      });
      return;
    }

    // 其他订单
    let size = 10;
    let data = await quest({
      url: "express/byDeliveryId/" + index + "?size=" + size,
    });
    if (data.code == 200) {
      this.setData({
        data: data.data.items,
        title,
        index,
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    let token = wx.getStorageSync("token");
    if (token != "") {
      this.getTitleData();
      this.getDataItems(this.data.size);
    }
  },

  async getTitleData() {
    let title = [];
    title.push({ id: 0, deliveryAddr: "全部", select: true });
    let { code, data } = await quest({
      url: "delivery/list/byUser",
    });
    // console.log("code=" + code);
    // console.log(data);
    if (code == 200) {
      data.items.forEach((element) => {
        Object.assign(element, { select: false });
        title.push(element);
      });
    }
    this.setData({
      title,
    });
  },
  // 获取用户所在大学的快递点
  async getDataItems(size) {
    let data = await quest({
      // url: "express/list/byUniversity?size=" + size,
      url: "express/list/byUniversity?size=" + size,
    });
    // console.log(data.data.items);
    if (data.code == 200) {
      this.setData({
        data: data.data.items,
        index: 0,
      });
    }
  },
  getDetail(e) {
    console.log(e.currentTarget.dataset.detail);
    let { status, detail } = e.currentTarget.dataset;
    console.log(JSON.stringify(detail));
    if (status == 1) {
      return;
    }
    wx.navigateTo({
      url: "/pages/detail/detail?detail=" + JSON.stringify(detail),
    });
  },
  addOrder(e) {
    console.log(e);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  async onReachBottom() {
    let size = this.data.size + 10;
    // 全部订单
    if (this.data.index == 0) {
      this.getDataItems(size);
      this.setData({
        size,
      });
    }
    // 其他订单
    else {
      let data = await quest({
        url: "express/byDeliveryId/" + index + "?size=" + size,
      });
      if (data.code == 200) {
        this.setData({
          data: data.data.items,
          index,
          size,
        });
      }
    }
    console.log("到底了");
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
  eject(e) {
    let content = e.currentTarget.dataset.content;
    console.log(content);
    this.setData({
      showModal: true,
      content,
    });
  },
  /**
   * 点击返回按钮隐藏
   */
  back: function () {
    this.setData({
      showModal: false,
    });
  },
  /**
   * 获取input输入值
   */
  wish_put: function (e) {
    this.setData({
      textV: e.detail.value,
    });
  },
  /**
   * 点击确定按钮获取input值并且关闭弹窗
   */
  async name() {
    // console.log(this.data.textV);
    if (this.data.textV.length !== 11) {
      return;
    }
    let claim = await this.getClaim();
    let data = await quest({
      url: "user/update",
      method: "post",
      data: {
        id: claim.uid,
        phone: this.data.textV,
      },
    });
    this.setData({
      showModal: false,
      name: this.data.textV,
    });
  },
});
